﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class EquipSimplePage : Page
    {
        public UILabel equipName;
        public UILabel equipQuality;
        public UILabel equipQualityValue;
        public UILabel equipStrengthLv;
        public UILabel equipRefineLv;
        public List<UILabel> equipPropertyWord;
        public List<UILabel> equipPropertyValue;
        public List<GameObject> lines;
        public GameObject pointerUp;
        public GameObject pointerDown;
        public GameObject iconRoot;
        public UISprite whiteBlock;
        
        EquipIcon equipIcon;

        //不再显示重塑
        public static bool dontShowRemodel = false;

        /// <summary>
        /// 0：装备，1：饰品
        /// </summary>
        int m_type;

        /// <summary>
        /// 当前选择的装备
        /// </summary>
        string equipGuid;
        string treasureGuid;
        string m_id;

        /// <summary>
        /// 当前选择的阵容位
        /// </summary>
        int heroPosition;

        //  白色背景原本高度
        float whiteOriginalHeight;

        void Awake()
        {
            Vector2 size = whiteBlock.GetComponent<UIWidget>().localSize;
            whiteOriginalHeight = size.y;
        }
        
        public void SetEquip(string guid)
        {
            equipGuid = guid;
            UIEquipment equip = StaticData.playerData.GetUIEquipment(guid);
            m_id = equip.equipId;

            // icon
            if(equipIcon == null)
            {
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                GameObject icon = Instantiate(obj);
                equipIcon = icon.GetComponent<EquipIcon>();
                icon.transform.parent = iconRoot.transform;
                icon.transform.localPosition = new Vector3(-88, 0, 0);
                icon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
            }
            equipIcon.SetEquip(equip);


            // 基础信息
            equipName.text = equip.GetWeaponNameWithHero(equip.lastHeroId); //TableTextBlo.GetText("name_" + equip.equipId).Replace("\\r", "\r").Replace("\\n", "\n");
            equipQuality.text = equip.GetQualityWord();
            equipQuality.color = StaticData.playerData.InstPlayerData.GetQualityColor(equip.equipData.quality);
            equipQualityValue.text = equip.equipData.quality.ToString();
            equipStrengthLv.text = equip.level.ToString() + "/" + StaticData.playerData.maxEquipLevel.ToString();
            equipRefineLv.text = equip.refineLv.ToString();

            // 品质变化箭头
            pointerUp.SetActive(false);
            pointerDown.SetActive(false);
            UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
            UIEquipment outEquip = formation.equipments[equip.equipData.type - 1];
            if(outEquip != null)
            {
                if (outEquip.equipData.quality < equip.equipData.quality)
                {
                    pointerUp.SetActive(true);
                }
                else if (outEquip.equipData.quality > equip.equipData.quality)
                {
                    pointerDown.SetActive(true);
                }
            }
            else
            {
                pointerUp.SetActive(true);
            }

            // 战斗属性
            int count = 0;
            for(int i = 0; i < equip.CurrentAttribute.attributes.Length; i++)
            {
                if(equip.CurrentAttribute.attributes[i] > 0 && count < 3)
                {
                    string propId = BaseAttributeData.GetDictPropIdByIndex(i);
                    equipPropertyWord[count].text = TableTextBlo.GetText("word_" + propId).Replace("\\r", "\r").Replace("\\n", "\n");
                    if(i >= 4)
                        equipPropertyValue[count].text = "+" + (equip.CurrentAttribute.attributes[i]).ToString() + "%";
                    else
                        equipPropertyValue[count].text = "+" + ((int)equip.CurrentAttribute.attributes[i]).ToString();

                    count++;
                }
            }

            //todo !!!!!!!! 不足三个时隐藏后面的。。
            for (int i = 0; i < lines.Count; i++)
            {
                lines[i].SetActive(true);
            }
            for (int i = count; i < equipPropertyWord.Count; i++)
            {
                equipPropertyValue[i].text = "";
                equipPropertyWord[i].text = "";
                lines[i].SetActive(false);
            }
            
            whiteBlock.GetComponent<UIWidget>().height = (int)(whiteOriginalHeight - (equipPropertyWord.Count - count) * 31);
        }

        public void SetTreasure(string guid)
        {
            treasureGuid = guid;
            UITreasure treasure = StaticData.playerData.GetUITreasure(guid);
            m_id = treasure.treasureId;

            // icon
            if (equipIcon == null)
            {
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                GameObject icon = Instantiate(obj);
                equipIcon = icon.GetComponent<EquipIcon>();
                icon.transform.parent = iconRoot.transform;
                icon.transform.localPosition = new Vector3(-88, 0, 0);
                icon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
            }
            equipIcon.SetTreasure(treasure);


            // 基础信息
            equipName.text = TableTextBlo.GetText("name_" + treasure.treasureId).Replace("\\r", "\r").Replace("\\n", "\n"); 
            equipQuality.text = treasure.GetQualityWord();
            equipQuality.color = StaticData.playerData.InstPlayerData.GetQualityColor(treasure.treasureData.quality);
            equipQualityValue.text = treasure.treasureData.quality.ToString();
            equipStrengthLv.text = treasure.strengthLevel.ToString() + "/" + StaticData.playerData.maxTreaLevel.ToString();
            equipRefineLv.text = treasure.refineLevel.ToString();

            // 品质变化箭头
            pointerUp.SetActive(false);
            pointerDown.SetActive(false);
            UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
            int index = treasure.treasureData.type == 1 ? 0 : 1;
            UITreasure outTreasure = formation.treasures[index];
            if (outTreasure != null)
            {
                if (outTreasure.treasureData.quality < treasure.treasureData.quality)
                {
                    pointerUp.SetActive(true);
                }
                else if (outTreasure.treasureData.quality > treasure.treasureData.quality)
                {
                    pointerDown.SetActive(true);
                }
            }
            else
            {
                pointerUp.SetActive(true);
            }

            // 战斗属性
            int count = 0;
            for (int i = 0; i < treasure.CurrentAttribute.attributes.Length; i++)
            {
                if (treasure.CurrentAttribute.attributes[i] > 0 && count < 3)
                {
                    string propId = BaseAttributeData.GetDictPropIdByIndex(i);
                    equipPropertyWord[count].text = TableTextBlo.GetText("word_" + propId).Replace("\\r", "\r").Replace("\\n", "\n");

                    if (i >= 4)
                        equipPropertyValue[count].text = "+" + treasure.CurrentAttribute.attributes[i].ToString() + "%";
                    else
                        equipPropertyValue[count].text = "+" + ((int)treasure.CurrentAttribute.attributes[i]).ToString();

                    count++;
                }
            }

            //todo !!!!!!!! 不足三个时隐藏后面的。。
            for (int i = 0; i < lines.Count; i++)
            {
                lines[i].SetActive(true);
            }
            for (int i = count; i < equipPropertyWord.Count; i++)
            {
                equipPropertyValue[i].text = "";
                equipPropertyWord[i].text = "";
                
                lines[i].SetActive(false);
            }
            
            whiteBlock.GetComponent<UIWidget>().height = (int)(whiteOriginalHeight - (equipPropertyWord.Count - count) * 31);
        }

        public void OnEquipBtn()
        {
            CSFormationOn msg = new CSFormationOn();
            if(m_type == 0)
            {
                UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
                UIEquipment equip = StaticData.playerData.GetUIEquipment(equipGuid);

                if (equip.equipData.type == 1 && equip.lastHeroId.CompareTo(formation.hero.heroId) != 0 && !dontShowRemodel)
                {
                    Close();
                    string options = "guid=" + equipGuid + "&heroPosition=" + heroPosition.ToString();
                    pageManager.OpenPage("EquipRemodelPage", options);
                    return;
                }

                msg.guid = equipGuid;
                msg.id = equip.equipId;
                msg.formatPos = heroPosition;
            }
            else
            {
                if (StaticData.playerData.level < Dict.Blo.DictConfigBlo.GetPlunderPVPLimitLevel())
                {
                    BoxManager.CreatePopupTextBox(string.Format(TableTextBlo.GetText("treasure_equip_level_limit"), Dict.Blo.DictConfigBlo.GetPlunderPVPLimitLevel()));
                    return;
                }
                UITreasure treasure = StaticData.playerData.GetUITreasure(treasureGuid);
                msg.guid = treasureGuid;
                msg.id = treasure.treasureId;
                msg.formatPos = heroPosition;
            }
           
            NetAPI.SendHttp(OpDefine.CSFormationOn, msg, EquipCallback);
        }

        void EquipCallback(NetWork.Packets.Packet packet, bool bSuccess)
        {
            Debug.Log("recv equip a equip...");
            if (bSuccess)
            {
                SCFormationOn msg = packet.kBody as SCFormationOn;
                if (msg != null)
                {
                    if(m_type == 0)
                    {
                        UIEquipment equip = StaticData.playerData.GetUIEquipment(equipGuid);
                        UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
                        formation.UpdateEquip(equipGuid, equip.equipData.type);
                    }
                    else
                    {
                        UITreasure treasure = StaticData.playerData.GetUITreasure(treasureGuid);
                        UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
                        formation.UpdateTreasure(treasureGuid, 6 - treasure.treasureData.type);
                    }
                    
                    Close();
                }
              
            }
        }

        public void OnDetailBtn()
        {
            string type = m_type == 0 ? "1" : "2";
			string guid = m_type == 0 ? equipGuid : treasureGuid;

			PageManager.Instance.OpenPage("EquipmentUIPage", "equipGuid=" + guid + "&type=" + type + "&equipId=" + m_id + "&heroId=" + "&heroPostion=");
        }

        override protected void DoOpen()
        {
            heroPosition = int.Parse(options["index"]);
            
            if(options.ContainsKey("equipGuid"))
            {
                m_type = 0;
                SetEquip(options["equipGuid"]);
            }
            else if(options.ContainsKey("treasureGuid"))
            {
                m_type = 1;
                SetTreasure(options["treasureGuid"]);
            }
        }

        override protected void DoClose()
        {

        }
    }
}
